import 'dart:async';
import 'dart:io';
import 'package:audio_waveforms/audio_waveforms.dart';
import 'package:flutter/material.dart';
import 'package:hockey/jzx_apps.dart';

class WaveBubble extends StatefulWidget {
  final String? path;
  final double? width;

  const WaveBubble({
    Key? key,
    this.width,
    this.path,
  }) : super(key: key);

  @override
  State<WaveBubble> createState() => _WaveBubbleState();
}

class _WaveBubbleState extends State<WaveBubble> {
  File? file;

  late PlayerController controller;
  late StreamSubscription<PlayerState> playerStateSubscription;

  final playerWaveStyle = const PlayerWaveStyle(
    fixedWaveColor: Colors.white54,
    liveWaveColor: Colors.white,
    spacing: 6,
  );

  @override
  void initState() {
    controller = PlayerController();
    _preparePlayer();
    playerStateSubscription = controller.onPlayerStateChanged.listen((_) {
      setState(() {});
    });
    super.initState();
  }

  void _preparePlayer() async {
    // final appDirectory = await getApplicationDocumentsDirectory();
    // Opening file from assets folder
    // if (widget.index != null) {
    // file = File('${appDirectory.path}/audio.mp3');
    // await file?.writeAsBytes(
    //     (await rootBundle.load('assets/audio.mp3')).buffer.asUint8List());
    // }
    if (widget.path == null && file?.path == null) {
      return;
    }
    // Prepare player with extracting waveform if index is even.
    controller.preparePlayer(
      path: widget.path ?? file!.path,
      shouldExtractWaveform: true,
    );
    // Extracting waveform separately if index is odd.
    // if (widget.index?.isOdd ?? false) {
    // controller
    //     .extractWaveformData(
    //       path: widget.path ?? file!.path,
    //       noOfSamples:
    //           playerWaveStyle.getSamplesForWidth(widget.width ?? 194.w),
    //     )
    //     .then((waveformData) => debugPrint(waveformData.toString()));
    // }
    setState(() {});
  }

  @override
  void dispose() {
    playerStateSubscription.cancel();
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return widget.path != null || file?.path != null
        ? Container(
            padding: const EdgeInsets.symmetric(horizontal: 6),
            margin: EdgeInsets.only(top: 8, left: 29.w),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(100),
                gradient: LinearGradient(
                  colors: ['#FF3FE1D8'.hexColor, '#FF63FF6A'.hexColor],
                )),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                if (!controller.playerState.isStopped)
                  IconButton(
                    onPressed: () async {
                      controller.playerState.isPlaying
                          ? await controller.pausePlayer()
                          : await controller.startPlayer(
                              finishMode: FinishMode.pause,
                            );
                    },
                    icon: Icon(
                      controller.playerState.isPlaying
                          ? Icons.stop
                          : Icons.play_arrow,
                    ),
                    color: Colors.white,
                    splashColor: Colors.transparent,
                    highlightColor: Colors.transparent,
                  ),
                AudioFileWaveforms(
                  size: Size(194.w, 20.w),
                  playerController: controller,
                  waveformType: WaveformType.long,
                  playerWaveStyle: playerWaveStyle,
                ),
              ],
            ),
          )
        : const SizedBox.shrink();
  }
}
